package com.taijihuabao.modules.controller;

import com.alibaba.excel.EasyExcel;
import com.taijihuabao.common.excel.EasyExcelListerner;
import com.taijihuabao.common.jsonresult.JsonResult;
import com.taijihuabao.common.commonutils.PageResult;
import com.taijihuabao.modules.entity.ChaimDetailEntity;
import com.taijihuabao.modules.entity.ChaimEntity;
import com.taijihuabao.modules.model.basedto.PageDTO;
import com.taijihuabao.modules.model.chaim.dto.ChaimInsertDTO;
import com.taijihuabao.modules.model.chaim.vo.ChaimHtmlVo;
import com.taijihuabao.modules.service.ChaimService;
import com.taijihuabao.modules.service.impl.ChaimServiceImpl;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 报销
 * @author xuejingtai
 */
@Api(tags = "报销")
@RestController
@RequestMapping("/api/v1/chaim")
public class ChaimController {

    @Autowired
    private ChaimService chaimService;
    @Autowired
    private Configuration configuration;

    public static String chaimCode;

    @ApiOperation("增加报销信息")
    @PostMapping(value = "/insertChaim")
    public JsonResult insertChaim(@RequestBody ChaimInsertDTO chaimInsertDTO) throws IOException {
        chaimService.insertChaim(chaimInsertDTO);
        return JsonResult.success();
    }

    @ApiOperation("查看自己的所有报销申请")
    @PostMapping("/selectChaimEntity")
    public JsonResult selectChaimEntity(@RequestBody PageDTO pageDTO){
        PageResult pageResult = chaimService.selectChaimEntity(pageDTO);
        return JsonResult.success(pageResult);
    }

    @ApiOperation("查看单个报销申请及详情")
    @PostMapping("/selectChaimDetail")
    public JsonResult selectChaimDetail(@RequestBody ChaimEntity chaimEntity){
        ChaimHtmlVo data = chaimService.selectById(chaimEntity.getId());
        return JsonResult.success(data);
    }

    @ApiOperation("导出数据以html形式")
    @GetMapping("/exportHtml")
    public JsonResult exportHtml (Long id) throws IOException, TemplateException {
        //freemaker路径
        String dir="E:\\IDEA-Workspace\\work_environment\\src\\main\\resources\\templates";
        //加载模板文件
        configuration.setDirectoryForTemplateLoading(new File(dir));
        //加载模板
        Template template = configuration.getTemplate("/chaimDetail.ftl");
        //往模板里添加的数据
        ChaimHtmlVo chaimHtmlVo = chaimService.selectById(id);

        System.out.println(chaimHtmlVo.getChaimEntity().getChaimComment());
        Map<String,Object>map = new HashMap<>(16);
        map.put("chaimHtmlVo",chaimHtmlVo);
        //定义输出流
        String outPath="C:\\Users\\26638\\Desktop\\chaim.html";
        FileWriter writer = new FileWriter(outPath);

        template.process(map,writer);
        writer.flush();
        writer.close();
        return JsonResult.success(chaimHtmlVo);
    }

    @ApiOperation("导入excel")
    @PostMapping("/importExcel")
    public JsonResult importExcel(ChaimInsertDTO chaimInsertDTO) throws IOException{
        MultipartFile file = chaimInsertDTO.getFile();
        chaimCode = ChaimServiceImpl.getUUID_16();
        EasyExcel.read(file.getInputStream(), ChaimDetailEntity.class, new EasyExcelListerner(chaimService)).sheet().doRead();
        return JsonResult.success(chaimCode);
    }

    @ApiOperation("删除excel")
    @PostMapping("/deleteExcel")
    public JsonResult deleteExcel(@RequestBody ChaimInsertDTO chaimInsertDTO) throws IOException{
        chaimService.deleteByChaimCode(chaimInsertDTO.getChaimCode());
        return JsonResult.success(chaimCode);
    }

    @ApiOperation("导出excel")
    @PostMapping("/exportExcel")
    public JsonResult exportExcel(){
        chaimService.exportExcel();
        return JsonResult.success();
    }

    @ApiOperation("删除报销申请")
    @PostMapping("/deleteChaim")
    public JsonResult deleteChaim(@RequestBody ChaimEntity chaimEntity){
        chaimService.deleteById(chaimEntity.getChaimCode());
        return JsonResult.success();
    }
}
